//
//  OrderCenterViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/9.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "HMSegmentedControl.h"
#import "TTPageViewController.h"
#import "OrderCenterViewController.h"
#import "OrderSubListViewController.h"

@interface OrderCenterViewController ()<TTPageViewControllerDelegate,
TTPageViewControllerDataSource>

@property (strong, nonatomic) HMSegmentedControl *segmentedControl;
@property (strong, nonatomic) TTPageViewController *containerController;

//@property (nonatomic, strong) UISearchBar *searchBar;

@property (assign, nonatomic) NSInteger currentIndex;
@property (nonatomic, strong) NSMutableArray *viewControllers;

@property (nonatomic, strong) NSMutableArray *cfgTitleArray;

@end

@implementation OrderCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self setIndex:self.selectIndex];
}

- (void)setIndex: (NSInteger)index {
    __weak typeof(self) weakSelf = self;
    weakSelf.currentIndex = index;
    weakSelf.containerController.currentIndex = index;
}

- (void)initView {
    
    self.title = @"订单中心";
    self.view.backgroundColor = UIColorHex(0xf1f1f1);
    WEAKSELF
    [self.view addSubview:self.segmentedControl];
    
    [self.segmentedControl setIndexChangeBlock:^(NSInteger index) {
        weakSelf.currentIndex = index;
        weakSelf.containerController.currentIndex = index;
    }];
    
    _containerController = [[TTPageViewController alloc]init];
    _containerController.delegate = self;
    _containerController.dataSource = self;
    _containerController.scrollEnabled = YES;
    
    [self addChildViewController:_containerController];
    [self.view addSubview:_containerController.view];
    [self initSubControllers];
    
//    [self initNaviBtn];
//    [self initPrivacy];
}


- (void)initSubControllers {
    if (!_viewControllers) {
        _viewControllers = [[NSMutableArray alloc] init];
    }
    
    for (int i = 0; i < self.cfgTitleArray.count; i++) {
        
        OrderSubListViewController *controller = [[OrderSubListViewController alloc] initWithType:i];
        [_viewControllers addObject:controller];
    }
}

#pragma mark - TTPageViewControllerDataSource
- (NSUInteger)pageCountForPageViewController:(TTPageViewController *)controller {
    
    return self.viewControllers.count;
}

- (UIViewController *)viewControllerForIndex:(NSUInteger)index forPageViewController:(TTPageViewController *)controller {

    return self.viewControllers[index];
}

#pragma mark - TTPageViewControllerDelegate
- (void)changedIndexOffset:(CGFloat)indexOffset forPageViewController:(TTPageViewController *)controller {
    
}

- (void)changedIndex:(NSUInteger)index forPageViewController:(TTPageViewController *)controller {
    [_segmentedControl setSelectedSegmentIndex:index animated:YES];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    _segmentedControl.frame = CGRectMake(0,0, TTScreenWidth, 45);
    
    [self.containerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.segmentedControl.mas_bottom);
        make.left.bottom.right.mas_equalTo(0);
    }];
}

- (HMSegmentedControl *)segmentedControl {
    
    if (!_segmentedControl) {
        
        _segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:self.cfgTitleArray.mutableCopy];
        _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
        _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
        _segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleFixed;
        
        _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorHex(0x333333), NSFontAttributeName :[UIFont systemFontOfSize:14]};
        _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName:UIColorHex(0x24c77e),NSFontAttributeName : [UIFont boldSystemFontOfSize:14]};
        _segmentedControl.selectionIndicatorColor = UIColorHex(0x24c77e);
        _segmentedControl.backgroundColor = [UIColor whiteColor];
        _segmentedControl.selectionIndicatorHeight = 2;
                
    }
    return _segmentedControl;
}

- (NSMutableArray *)cfgTitleArray {
    if (!_cfgTitleArray) {
        _cfgTitleArray = @[@"全部",@"待付款",@"待接单",@"待服务",@"待退款",@"已完成"].mutableCopy;
    }
    return _cfgTitleArray;
}

@end
